package cn.shyouchi.wuguanjia.android.ui.patrol.report;

import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import cn.shyouchi.wuguanjia.android.R;

/**
 * Created by Eenie on 2016/12/28 at 14:33
 * Email: 472279981@qq.com
 * Des:
 */

public class SelRepairLeaderAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int REPAIR_ITEM_TYPE_GROUP = 0;
    public static final int REPAIR_ITEM_TYPE_SUBITEM = 1;


    public SelRepairLeaderAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(REPAIR_ITEM_TYPE_GROUP, R.layout.item_sel_repair_group_layout);
        addItemType(REPAIR_ITEM_TYPE_SUBITEM, R.layout.item_sel_repair_subitem_layout);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity entity) {
        switch (entity.getItemType()) {
            case REPAIR_ITEM_TYPE_GROUP:
                final RepairLeaderGroup leaderGroup = (RepairLeaderGroup) entity;
                holder.setText(R.id.tv_group_name, leaderGroup.getName());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (leaderGroup.isExpanded()) {
                            collapse(holder.getAdapterPosition());
                        } else {
                            expand(holder.getAdapterPosition());
                        }
                    }
                });


                break;
            case REPAIR_ITEM_TYPE_SUBITEM:
                final RepairLeaderSubitem leaderSubitem = (RepairLeaderSubitem) entity;
                holder.setText(R.id.tv_subitem_name, leaderSubitem.getName());
                final CheckBox cb = holder.getView(R.id.tv_subitem_name);
                cb.setChecked(leaderSubitem.isChecked());
                cb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        leaderSubitem.setChecked(!leaderSubitem.isChecked());
                        notifyItemChanged(holder.getAdapterPosition());
                    }
                });
                break;
        }

    }



    public List<RepairLeaderSubitem> getSelLeaderList() {

        List<RepairLeaderSubitem> sels = new ArrayList<>();

        for (MultiItemEntity entity : getData()) {
            if (entity instanceof RepairLeaderSubitem && ((RepairLeaderSubitem) entity).isChecked()) {
                sels.add((RepairLeaderSubitem) entity);
            }
        }
        return sels;
    }


}
